package CCRMClient.GUI;

import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Component;

import javax.swing.border.LineBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;

import java.awt.SystemColor;

import javax.swing.JTextField;
import javax.swing.JButton;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.CatalogManager;
import CCRMClient.Controllers.ClientCustomerManagement;
import CCRMServer.DBConnect;
import Common.SERVER_COMMANDS;
import Common.Entities.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;



public class AddCustomerPermission extends JPanel {
	
	private JLabel lblAddCustomerPermission;
	private JLabel lblCategoryName;
	private JLabel lblPermissionType;
	private JTextField textCategoryName;
	private JTextField textCustomerID;
	private JButton btnSubmit;
	private JLabel lblCustomerId;
	private JButton btnGoBack;
	private JComboBox cmbPermissionType;
	private JLabel lblNewLabel;
	
	/**
	 * This enum enumrates permission types for the GUI combo
	 * @author ori ziv
	 *
	 */
	public enum PermissionType {
		ALLOWED, NOT_ALLOWED;
		public static String toString(PermissionType pt) {
			switch (pt) {
			case ALLOWED:
				return "ALLOWED";
			case NOT_ALLOWED:
				return "NOT_ALLOWED";
			}
			return null;
		}
		public static int toInt(PermissionType pt) {
			switch (pt) {
			case ALLOWED:
				return 1;
			case NOT_ALLOWED:
				return 2;
			}
			return -1;
		} 
	}
	
	public AddCustomerPermission() {
			
			initialize();
		}
	
	private void initialize(){
		setSize (507,285);
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setBackground(Color.WHITE);
		setLayout(null);
		
		add(getLblAddCustomerPermission());
		add(getLblCategoryName());
		add(getLblPermissionType());
		add(getTextCategoryName());
		add(getTextCustomerID());
		add(getBtnSubmit());
		add(getLblCustomerId());
		add(getBtnGoBack());
		add(getCmbPermissionType());
		add(getLblNewLabel());
	}
	
	JLabel getLblAddCustomerPermission(){
		if(lblAddCustomerPermission==null){
			lblAddCustomerPermission=new JLabel("Add Permission");
			lblAddCustomerPermission.setHorizontalAlignment(SwingConstants.CENTER);
			lblAddCustomerPermission.setForeground(Color.RED);
			lblAddCustomerPermission.setFont(new Font("David", Font.BOLD, 26));
			lblAddCustomerPermission.setBackground(Color.WHITE);
			lblAddCustomerPermission.setBounds(0, 0, 507, 39);
		}
		return lblAddCustomerPermission;
	}
	
	JLabel getLblCategoryName(){
		if(lblCategoryName==null){
			lblCategoryName=new JLabel("CategoryName");
			lblCategoryName.setHorizontalAlignment(SwingConstants.LEFT);
			lblCategoryName.setForeground(Color.BLACK);
			lblCategoryName.setFont(new Font("David", Font.BOLD, 20));
			lblCategoryName.setBackground(Color.WHITE);
			lblCategoryName.setBounds(10, 71, 139, 29);
		}
		return lblCategoryName;
	}
	
	JLabel getLblPermissionType(){
		
		if(lblPermissionType==null){
			lblPermissionType=new JLabel("Permission Type");
			lblPermissionType.setFont(new Font("David", Font.BOLD, 20));
			lblPermissionType.setBounds(10,151, 164, 29);
		}
		return lblPermissionType;
		
	}
	JTextField getTextCustomerID() {
		if (textCustomerID == null) {
			textCustomerID = new JTextField();
			textCustomerID.setColumns(10);
			textCustomerID.setBackground(SystemColor.menu);
			textCustomerID.setBounds(192, 116, 251, 20);
		}
		
		return textCustomerID ;
	}
	
	JTextField getTextCategoryName() {
		if (textCategoryName == null) {
			textCategoryName = new JTextField();
			textCategoryName.setColumns(10);
			textCategoryName.setBackground(SystemColor.menu);
			textCategoryName.setBounds(192, 76, 251, 20);
		}
		
		return textCategoryName ;
	}
	JButton getBtnSubmit(){
		if (btnSubmit == null) {
			btnSubmit = new JButton("Submit");
			btnSubmit.addActionListener(new BtnSubmitActionListener());
			btnSubmit.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnSubmit.setBounds(318, 207, 125, 23);
		}
		return btnSubmit;
		
	}
	
	/**
	 * This inner class implements event listener for pressing Submit button
	 * @author Ori Ziv
	 *
	 */
	private class BtnSubmitActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			try {
				String categoryName = textCategoryName.getText();
				String customerID = textCustomerID.getText();
				int permissionType = AddCustomerPermission.PermissionType.toInt((PermissionType)cmbPermissionType.getSelectedItem());
				if (categoryName.isEmpty() || customerID.isEmpty()) throw new NullPointerException();
				try {
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.ADD_PERMISSION, ClientCustomerManagement.addPermission(customerID, categoryName, permissionType));
				} catch (IOException exp) {
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();
				}
			} catch (NullPointerException e) {
				JOptionPane.showMessageDialog(null, "All fields are mandatory", "Add Permission", JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
	/**
	 * This inner class implements event listener for pressing Go Back button
	 * @author Ori Ziv
	 *
	 */
	private class BtnGoBackActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			ClientStart.panelSwicher(new CustomerManage());
		}
	}
		private JLabel getLblCustomerId() {
			if (lblCustomerId == null) {
				lblCustomerId = new JLabel("Customer ID");
				lblCustomerId.setHorizontalAlignment(SwingConstants.LEFT);
				lblCustomerId.setForeground(Color.BLACK);
				lblCustomerId.setFont(new Font("David", Font.BOLD, 20));
				lblCustomerId.setBackground(Color.WHITE);
				lblCustomerId.setBounds(10, 111, 139, 29);
			}
			return lblCustomerId;
		}
		private JButton getBtnGoBack() {
			if (btnGoBack == null) {
				btnGoBack = new JButton("Go Back");
				btnGoBack.addActionListener(new BtnGoBackActionListener());
				btnGoBack.setFont(new Font("Tahoma", Font.BOLD, 12));
				btnGoBack.setBounds(10, 207, 125, 23);
			}
			return btnGoBack;
		}
		private JComboBox getCmbPermissionType() {
			if (cmbPermissionType == null) {
				cmbPermissionType = new JComboBox();
				cmbPermissionType.setModel(new DefaultComboBoxModel(PermissionType.values()));
				cmbPermissionType.setSelectedIndex(0);
				cmbPermissionType.setBounds(192, 156, 251, 20);
			}
			return cmbPermissionType;
		}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("All fields are mandatory!");
			lblNewLabel.setForeground(Color.RED);
			lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
			lblNewLabel.setBounds(10, 46, 192, 14);
		}
		return lblNewLabel;
	}
}


	
	


